package cn.wangzg.datastoredemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb_rem;
    private CheckBox cb_dis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
        cb_rem=findViewById(R.id.checkBox);

        SharedPreferences mysp = getSharedPreferences("mysp", MODE_PRIVATE);
        String name = mysp.getString("name", "");
        String pwd = mysp.getString("pwd", "");
        et_name.setText(name);
        et_pwd.setText(pwd);

        cb_dis = findViewById(R.id.checkBox2);

        cb_dis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

                if(isChecked){
                    //如果选中，显示密码
                    et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }
    public void login(View view){
        String name = et_name.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();
        if("lisi".equals(name)&& "123456".equals(pwd)){
            if(cb_rem.isChecked()){
                SharedPreferences mysp = getSharedPreferences("mysp", MODE_PRIVATE);
                SharedPreferences.Editor edit = mysp.edit();
                edit.putString("name",name);
                edit.putString("pwd",pwd);
                //edit.commit();
                edit.apply();
            }
            Intent intent=new Intent(this,OtherActivity.class);
            startActivity(intent);
        }else {
            Toast.makeText(this, "用户名或者密码错误！",
                    Toast.LENGTH_SHORT).show();
        }
    }
}